package com.example.zhunongzonghe.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.zhunongzonghe.beans.ZnMoresBean;
import com.example.zhunongzonghe.common.Result;
import com.example.zhunongzonghe.entity.ZnMores;
import com.example.zhunongzonghe.service.IZnMoresService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;


@RestController
@RequestMapping("/znMores")
public class ZnMoresController {
    @Autowired
    private IZnMoresService iznMoresService;

    @GetMapping("/SearchMoresAll")
    public Result<Page> SearchMoresAll(@RequestParam("currentPage") Integer currentPage,
                                       @RequestParam("pageSize") Integer pageNum,
                                       @RequestParam("input") Object Search){
        Page<ZnMoresBean> znMoresPage = iznMoresService.SearchMoresAll(currentPage,pageNum,Search);
        return new Result<>(true,"查询成功",200,znMoresPage);
    }

    /**
     * 前台查询全部
     * @param currentPage
     * @param pageNum
     * @return
     */
    @GetMapping("/selectMoresAll")
    public Result<Page> selectMoresAll(@RequestParam("currentPage") Integer currentPage,
                                       @RequestParam("pageSize") Integer pageNum){
        Page<ZnMoresBean> znMoresPage = iznMoresService.selectMoresAll(currentPage,pageNum);
        return new Result<>(true,"查询成功",200,znMoresPage);
    }

    @PutMapping("/editorMores")
    public Result<ZnMores> editorMores(@RequestBody ZnMores znMores){
        znMores.setUpdateTime(LocalDateTime.now());
        iznMoresService.updateById(znMores);
        return new Result<>(true,"编辑成功",200);
    }

    @PostMapping("/addMores")
    public Result<ZnMores> addMores(@RequestBody ZnMores znMores){
        znMores.setCreateTime(LocalDateTime.now());
        iznMoresService.save(znMores);
        return new Result<>(true,"成功",200);
    }

    @DeleteMapping("/deleteMores/{deleteMores}")
    public Result<ZnMores> deleteMores(@PathVariable Integer deleteMores){
        iznMoresService.removeById(deleteMores);
        return new Result<>(true,"删除成功",200);
    }

    /**
     * 单个风俗文化信息
     * @param moresId
     * @return
     */
    @GetMapping("/getMoresInfo/{moresId}")
    public Result getMoresInfo(@PathVariable Integer moresId){
        if (moresId==null) return new Result<>(false,"数据异常",400);
        ZnMoresBean znMoresBean = iznMoresService.getMoresInfo(moresId);
        return new Result<>(true,"成功",200,znMoresBean);
    }

    /**
     * 通过分类id查询风俗文化page集合
     * @param currentPage
     * @param pageNum
     * @param classifyId
     * @return
     */
    @GetMapping("/selectClassifyId")
    public Result<Page> selectClassifyId(@RequestParam("currentPage") Integer currentPage,
                                       @RequestParam("pageSize") Integer pageNum,
                                       @RequestParam("classifyId") Integer classifyId){
        if (classifyId==null) return new Result<>(false,"数据异常",400);
        Page<ZnMoresBean> znMoresPage = iznMoresService.selectClassifyId(currentPage,pageNum,classifyId);
        return new Result<>(true,"查询成功",200,znMoresPage);
    }
}